package com.db.collections;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ArrayListVsLinkedListBenchmark {
  
  private final static List<String> array = new ArrayList<>();
  private final static List<String> linked = new LinkedList<>();
  
  private static void warmup(long millis) {
    long begin = System.nanoTime();
    do {
      array.add(0, "" + Math.random());
      array.remove(0);
      linked.add(0, "" + Math.random());
      linked.remove(0);
    } while (System.nanoTime() - begin < millis * 1_000_000);
  }
  
  public static void main(String[] args) {
    warmup(10_000);
    System.out.format("%s\t%s\t%s%n", "size", "array", "linked");
    long arrayTime = 0;
    long linkedTime = 0;
    do {
      arrayTime = System.nanoTime();
      array.add(0, "" + Math.random());
      arrayTime = System.nanoTime() - arrayTime;
      
      linkedTime = System.nanoTime();
      linked.add(0, "" + Math.random());
      linkedTime = System.nanoTime() - linkedTime;
      
      if (array.size() % 1000 == 0)
        System.out.format("%d\t%d\t%d%n", array.size(), arrayTime, linkedTime);
    } while (arrayTime < 100 * linkedTime);
    
  }
}
